package net.daum.hibernate;
 
import java.io.File;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
 
public class HibernateUtil {
 
    private static SessionFactory sessionFactory = null;
 
    private static SessionFactory buildSessionFactory(File file) {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        	
        	
        	Configuration configuration = new AnnotationConfiguration();
        	if (file == null) {
        		return configuration.configure().buildSessionFactory();
        	} else {
        		return configuration.configure(file).buildSessionFactory();
        	}
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static SessionFactory getSessionFactory(File file) {
    	if (sessionFactory == null) {
    		sessionFactory = buildSessionFactory(file);
    	}
        return sessionFactory;
    }
    
    public static SessionFactory getSessionFactory() {
    	if (sessionFactory == null) {
    		sessionFactory = buildSessionFactory(null);
    	}
        return sessionFactory;
    }
}

